package com.draven.mall.controller;

import com.draven.mall.common.Result;
import com.draven.mall.entity.User;
import com.draven.mall.entity.Merchant;
import com.draven.mall.entity.Rider;
import com.draven.mall.service.UserService;
import com.draven.mall.service.MerchantService;
import com.draven.mall.service.RiderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@CrossOrigin
@Api(tags = "认证接口")
public class AuthController {

    @Autowired
    private UserService userService;

    @Autowired
    private MerchantService merchantService;

    @Autowired
    private RiderService riderService;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result<Map<String, Object>> login(@RequestBody Map<String, String> loginForm) {
        String username = loginForm.get("username");
        String password = loginForm.get("password");

        if (username == null || password == null) {
            return Result.error("用户名和密码不能为空");
        }

        User user = userService.login(username, password);
        if (user != null) {
            // 生成token
            String token = UUID.randomUUID().toString().replace("-", "");

            Map<String, Object> data = new HashMap<>();
            data.put("id", user.getId());
            data.put("username", user.getUsername());
            data.put("nickname", user.getNickname());
            data.put("phone", user.getPhone());
            data.put("email", user.getEmail());
            data.put("avatar", user.getAvatar());
            data.put("address", user.getAddress());
            data.put("token", token);
            data.put("role", "user");

            return Result.success(data);
        }

        return Result.error("用户名或密码错误");
    }

    @PostMapping("/merchant/login")
    @ApiOperation("商家登录")
    public Result<Map<String, Object>> merchantLogin(@RequestBody Map<String, String> loginForm) {
        String username = loginForm.get("username");
        String password = loginForm.get("password");

        if (username == null || password == null) {
            return Result.error("用户名和密码不能为空");
        }

        Merchant merchant = merchantService.login(username, password);
        if (merchant != null) {
            // 生成token
            String token = UUID.randomUUID().toString().replace("-", "");

            Map<String, Object> data = new HashMap<>();
            data.put("id", merchant.getId());
            data.put("username", merchant.getUsername());
            data.put("shopName", merchant.getShopName());
            data.put("phone", merchant.getPhone());
            data.put("address", merchant.getAddress());
            data.put("token", token);
            data.put("role", "merchant");

            return Result.success(data);
        }

        return Result.error("用户名或密码错误");
    }

    @PostMapping("/rider/login")
    @ApiOperation("骑手登录")
    public Result<Map<String, Object>> riderLogin(@RequestBody Map<String, String> loginForm) {
        String username = loginForm.get("username");
        String password = loginForm.get("password");

        if (username == null || password == null) {
            return Result.error("用户名和密码不能为空");
        }

        Rider rider = riderService.login(username, password);
        if (rider != null) {
            // 生成token
            String token = UUID.randomUUID().toString().replace("-", "");

            Map<String, Object> data = new HashMap<>();
            data.put("id", rider.getId());
            data.put("username", rider.getUsername());
            data.put("realName", rider.getRealName());
            data.put("phone", rider.getPhone());
            data.put("token", token);
            data.put("workingStatus", rider.getWorkingStatus());
            data.put("role", "rider");

            return Result.success(data);
        }

        return Result.error("用户名或密码错误");
    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Result<User> register(@RequestBody User user) {
        User registeredUser = userService.register(user);
        if (registeredUser != null) {
            // 不返回密码
            registeredUser.setPassword(null);
            return Result.success(registeredUser);
        }
        return Result.error("注册失败，用户名或手机号已存在");
    }

    /**
     * 测试登录接口
     */
    @GetMapping("/test/login")
    @ApiOperation("测试登录")
    public Result<Map<String, Object>> testLogin(@RequestParam String type, @RequestParam String username) {
        Map<String, Object> data = new HashMap<>();
        String token = UUID.randomUUID().toString().replace("-", "");

        data.put("token", token);
        data.put("role", type);

        if ("user".equals(type)) {
            data.put("id", 1001L);
            data.put("username", username);
            data.put("nickname", "张三");
            data.put("phone", "13812345678");
            data.put("email", "user001@example.com");
        } else if ("merchant".equals(type)) {
            data.put("id", 101L);
            data.put("username", username);
            data.put("shopName", "美味餐厅");
            data.put("phone", "18612345678");
        } else if ("rider".equals(type)) {
            data.put("id", 101L);
            data.put("username", username);
            data.put("realName", "王五");
            data.put("phone", "15812345678");
            data.put("workingStatus", 1);
        }

        return Result.success(data);
    }
}
